package iotwisp.school.api.controller.pc_admin.user;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.JSONUtils;
import iotwisp.school.user.entity.ApiOperate;
import iotwisp.school.user.entity.Permission;
import iotwisp.school.user.qo.ApiOperateQO;
import iotwisp.school.user.service.ApiOperateService;

@RestController
@RequestMapping("/api/org/api_operate")
public class ApiOperateController extends RestBaseController {

	@Autowired
	private ApiOperateService apiOperateService;

	@RequestMapping(value = "/query", method = { RequestMethod.POST, RequestMethod.GET })
	public ResponseEntity<JSONResultDTO> queryApiOperate(@RequestBody(required = false) ApiOperateQO qoBody,
			@RequestParam(name = "qo", required = false) String qoJson) throws UnsupportedEncodingException {

		ApiOperateQO qo;
		if (!StringUtils.isBlank(qoJson)) {
			qo = JSONUtils.parseObject(URLDecoder.decode(qoJson, "UTF-8"), ApiOperateQO.class);
		} else {
			qo = qoBody;
		}

		switch (qo.getResultType()) {
		case ApiOperateQO.RESULT_TYPE_COUNT:
			Integer count = apiOperateService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case ApiOperateQO.RESULT_TYPE_UNIQUE:
			ApiOperate apiOperate = apiOperateService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(apiOperate));
		case ApiOperateQO.RESULT_TYPE_LIST:
			List<ApiOperate> apiOperates = apiOperateService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(apiOperates));
		case ApiOperateQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = apiOperateService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		case ApiOperateQO.RESULT_TYPE_TREE:
			List<ApiOperate> apiOperateList = apiOperateService.queryList(qo);
			ApiOperate apiOperateTree = transToTree(apiOperateList);
			return ResponseEntity.ok(createSuccessResult(apiOperateTree));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}

	private ApiOperate transToTree(List<ApiOperate> apiOperates) {

		List<ApiOperate> list = new ArrayList<>();
		Map<String, List<Permission>> apiOperateChildren = new HashMap<>();

		for (ApiOperate apiOperate : apiOperates) {
			if (StringUtils.isNotBlank(apiOperate.getTreeData().getParentId())) {
				if (!apiOperateChildren.containsKey(apiOperate.getTreeData().getParentId())) {
					apiOperateChildren.put(apiOperate.getTreeData().getParentId(), new ArrayList<>());
				}
				apiOperateChildren.get(apiOperate.getTreeData().getParentId()).add(apiOperate);
			}
		}

		for (ApiOperate apiOperate : apiOperates) {
			if (apiOperateChildren.containsKey(apiOperate.getId())) {
				apiOperate.setChildren(apiOperateChildren.get(apiOperate.getId()));
				list.add(apiOperate);
			}
		}

		for (ApiOperate apiOperate : apiOperates) {
			if (StringUtils.equals(apiOperate.getId(), "api_root")) {
				return apiOperate;
			}
		}

		return null;
	}

}
